package com.jming.yygh.hosp.controller.api;

import com.jming.yygh.common.exception.YyghException;
import com.jming.yygh.common.helper.HttpRequestHelper;
import com.jming.yygh.common.result.Result;
import com.jming.yygh.common.result.ResultCodeEnum;
import com.jming.yygh.common.util.MD5;
import com.jming.yygh.hosp.service.DepartmentService;
import com.jming.yygh.hosp.service.HospitalService;
import com.jming.yygh.hosp.service.HospitalSetService;
import com.jming.yygh.hosp.service.ScheduleService;
import com.jming.yygh.model.hosp.Department;
import com.jming.yygh.model.hosp.Schedule;
import com.jming.yygh.vo.hosp.DepartmentQueryVo;
import com.jming.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author jming
 * @date 2021-04-22
 */
@Api(tags = "医院管理API接口")
//@CrossOrigin  // 允许跨域访问
@RestController   // Rest风格注入
@RequestMapping("/api/hosp")
public class ApiController {

    @Resource
    private HospitalService hospitalService;
    @Resource
    private HospitalSetService hospitalSetService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private ScheduleService scheduleService;

    /**
     * 删除排班信息
     *
     * @param request 删除的请求数据
     * @return 删除结果
     */
    @PostMapping("schedule/remove")
    public Result removoSchedule(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 根据医院编号和科室编号删除数据
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.removo(hoscode, hosScheduleId);

        return Result.ok();
    }

    /**
     * 查询排班信息
     *
     * @param request 查询的请求
     * @return 查询的结果
     */
    @PostMapping("schedule/list")
    public Result<Page<Schedule>> findSchedule(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 查询数据 获取当前页，每页记录数，判断如果不存在则默认赋值为1
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        // 获取医院编号、科室编号，并设置进ScheduleQueryVo中
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setHoscode(depcode);

        // 调用service方法查询
        Page<Schedule> pageModel = scheduleService
                .findPageSchedule(page, limit, scheduleQueryVo);

        return Result.ok(pageModel);
    }


    /**
     * 上传排班信息
     *
     * @param request 请求上传的数据
     * @return 返回上传的结果
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 上传数据
        scheduleService.save(paramMap);

        return Result.ok();
    }


    /**
     * 删除科室信息
     *
     * @param request 请求删除的数据
     * @return 删除结果
     */
    @PostMapping("department/remove")
    public Result removoDepartment(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 根据医院编号和科室编号删除数据
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        departmentService.removo(hoscode, depcode);

        return Result.ok();
    }


    /**
     * 查询科室信息
     *
     * @param request 请求查询的信息
     * @return 查询结果
     */
    @PostMapping("department/list")
    public Result<Page<Department>> findDepartment(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 查询数据 获取当前页，每页记录数，判断如果不存在则默认赋值为1
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        String hoscode = (String) paramMap.get("hoscode");
        departmentQueryVo.setHoscode(hoscode);
        // 调用service方法查询
        Page<Department> pageModel = departmentService
                .findPageDepartment(page, limit, departmentQueryVo);

        return Result.ok(pageModel);
    }


    /**
     * 上传科室信息
     *
     * @param request 请求上传的数据
     * @return 上传结果
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 传结果
        departmentService.save(paramMap);

        return Result.ok();
    }


    /**
     * 查询医院接口
     *
     * @param request 请求
     * @return 处理结果
     */
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result<com.jming.yygh.model.hosp.Hospital> hospital(HttpServletRequest request) {

        // 把request对象转换成Map对象
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 保获取数据并保存结果
        return Result.ok(hospitalService.getByHoscode((String) paramMap.get("hoscode")));

    }


    /**
     * 上传医院接口
     *
     * @param request 上传请求
     * @return 返回处理结果
     */
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {

        // 获取传递过来的医院信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名校验
        if (!this.isSign(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 传输过程中“+”转换为了“ ”，因此要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        String logoData = logoDataString.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        // 调用service方法保存数据
        hospitalService.save(paramMap);
        return Result.ok();
    }


    /**
     * 签名验证
     *
     * @param paramMap 传入验证请求
     * @return 验证结果
     */
    private boolean isSign(Map<String, Object> paramMap) {

        //1、获取医院编号，判断编号是否存在
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 2、获取医院系统传递过来的签名,做了MD5加密
        String hospSign = (String) paramMap.get("sign");

        // 3、根据传递过来的医院编号，查询数据库签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        // 4、把数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey.toString());

        // 5、签名校验
        if (!hospSign.equals(signKeyMd5)) {
            return false;
        }
        // 6、返回验证结果
        return true;
    }

}
